<?php

/*
 * Comentarios
 * Listado de Comentarios
 * ABM de comentarios
*/

class Comentarios extends Controller {

    function Comentarios() {
        parent::Controller();
        $this->load->library('session');
        if(!$this->session->userdata('logged_in'))
            redirect('/home/');
        $this->load->language('general',$this->session->userdata('lenguaje'));
    }

    function index() {
        $datos['titulo_pagina'] = 'Comentarios';
        $this->load->view('admin/encabezado_admin.php', $datos);
        $this->load->view('admin/menu_admin.php');
        $this->load->view('admin/comentarios.php');
        $this->load->view('admin/pie_admin.php');
    }

    function post() {
        $tabla       = "comentarios";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";


        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        //echo $sql;
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);
        $result = $query->result();


        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id;
            $data['rows'][$i]['cell'][]= $row->id;
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_noticia);
            $data['rows'][$i]['cell'][]= utf8_encode($row->texto);
            $data['rows'][$i]['cell'][]= utf8_encode($row->peso_2);
            $data['rows'][$i]['cell'][]= utf8_encode($row->usuario);
            $i++;
        }
        echo json_encode($data);
    }

    function guardar() {
        $tabla          = "comentarios";
        $oper           = $this->input->post('oper');

        $id             = $this->input->post('id');
        $id_noticia     = $this->input->post('id_noticia');
        $texto          = $this->input->post('texto');
        $peso_2         = $this->input->post('peso_2');
        $usuario        = $this->input->post('usuario');

        if($oper=='edit') {
            $query = $this->db->query("UPDATE $tabla set id_noticia='".$id_noticia."', texto='".$texto."', peso_2='".$peso_2."', usuario='".$usuario."' WHERE id=".$id);
        }elseif($oper=='add') {
            $query = $this->db->query("INSERT INTO $tabla set id_noticia='".$id_noticia."', texto='".$texto."', peso_2='".$peso_2."', usuario='".$usuario."'");
        }elseif($oper=='del') {
            $query = $this->db->query("DELETE FROM $tabla WHERE id=$id");
        }
    }

}


?>